package tests;

import common.Utils;
import org.junit.jupiter.api.Assertions;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;

import java.util.Set;

public class LoginPage extends Utils {
    private static String url = "http://8.134.99.135:58080/sign-in.html";

    public LoginPage() {
        super(url);
    }

    /**
     * 一、检查页面是否可以正常打开
     */
    public void checkLogin(){
        //检查log是否可以正常加载
        driver.findElement(By.cssSelector("body > div > div > div > div:nth-child(1) > div > div.text-center.mb-4 > img"));
        //检查登录表单是否可以正常加载
        driver.findElement(By.cssSelector("body > div > div > div > div:nth-child(1) > div > div.card.card-md > div"));
        //检查标题是否可以正常加载
        String loginText = driver.findElement(By.cssSelector("body > div > div > div > div:nth-child(1) > div > div.card.card-md " +
                "> div > h2")).getText();
        Assertions.assertEquals("用户登录",loginText);

        //检查输入框是否可以正常加载，并判断输入框里面的默认内容是否与预期的相同
        String nameTest = driver.findElement(By.cssSelector("#username")).getAttribute("placeholder");
        Assertions.assertEquals("请输入用户名",nameTest);
        String passwordText = driver.findElement(By.cssSelector("#password")).getAttribute("placeholder");
        Assertions.assertEquals("请输入密码",passwordText);

        ////检查注册按钮的内容是否正确，按钮是否可点击
        String text = driver.findElement(By.cssSelector("#submit")).getText();
        Assertions.assertEquals("登录",text);
        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#submit")));
    }



    /**
     * 二、异常登录
     */
    public void exceptionLogin(){
        //测试数据 用户名：test 密码：test
        //1、登录信息缺项
        //1、1 用户名为空
        driver.findElement(By.cssSelector("#password")).sendKeys("tests");
        driver.findElement(By.cssSelector("#submit")).click();
        String nameTest = driver.findElement(By.cssSelector("#signInForm > div.mb-3 > div")).getText();
        Assertions.assertEquals("用户名不能为空",nameTest);

        //1、2 密码名为空
        //清空输入框的内容
        driver.findElement(By.cssSelector("#password")).clear();

        driver.findElement(By.cssSelector("#username")).sendKeys("tests");
        driver.findElement(By.cssSelector("#submit")).click();
        String passwordText = driver.findElement(By.cssSelector("#signInForm > div.mb-2 > div > div")).getText();
        Assertions.assertEquals("密码不能为空",passwordText);

        //1、3 用户名和密码都为空
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#submit")).click();
        String nameTest13 = driver.findElement(By.cssSelector("#signInForm > div.mb-3 > div")).getText();
        Assertions.assertEquals("用户名不能为空",nameTest13);
        String passwordText13 = driver.findElement(By.cssSelector("#signInForm > div.mb-2 > div > div")).getText();
        Assertions.assertEquals("密码不能为空",passwordText13);


        //2、登录信息填写完整
        //2、1 用户名错误
        driver.findElement(By.cssSelector("#username")).sendKeys("111");
        driver.findElement(By.cssSelector("#password")).sendKeys("tests");
        driver.findElement(By.cssSelector("#submit")).click();
        String nameText = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right" +
                " > div:nth-child(1)")).getText();
        String[] s = nameText.split("\n");
        Assertions.assertEquals("用户不存在",s[2]);


        //2、2 密码错误
        //先清空输入框
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#password")).clear();
        driver.findElement(By.cssSelector("#username")).sendKeys("tests");
        driver.findElement(By.cssSelector("#password")).sendKeys("test111");
        driver.findElement(By.cssSelector("#submit")).click();
        String passwordText22 = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div:nth-child(2)")).getText();
        String[] s1 = passwordText22.split("\n");
        Assertions.assertEquals("用户名或密码错误",s1[2]);


        //2、3 用户名和密码都错误
        //先清空输入框
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#password")).clear();
        driver.findElement(By.cssSelector("#username")).sendKeys("111");
        driver.findElement(By.cssSelector("#password")).sendKeys("test11");
        driver.findElement(By.cssSelector("#submit")).click();
        String nameAndPsText = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > " +
                "div:nth-child(1)")).getText();
        String[] s3 = nameAndPsText.split("\n");
        Assertions.assertEquals("用户不存在",s3[2]);

    }


    /**
     * 三、正常登录
     */
    public void loginSuc(){
        //把输入框清空
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#password")).clear();
        //测试数据 用户名：test 密码：test
        driver.findElement(By.cssSelector("#username")).sendKeys("test");
        driver.findElement(By.cssSelector("#password")).sendKeys("test");
        driver.findElement(By.cssSelector("#submit")).click();

        //检查登录后跳转的页面是不是社区首页
        String currentHandle = driver.getWindowHandle();
        Set<String> handles = driver.getWindowHandles();
        for (String handle : handles){
            if (!handle.equals(currentHandle)){
                driver.switchTo().window(handle);
            }
        }
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#article_list_board_title")));
        Assertions.assertEquals("http://8.134.99.135:58080/index.html",driver.getCurrentUrl());
    }

}
